package com.wushi.lenovo.asia5b.app_ui.good.view;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_model.profile.AddressModel;
import com.wushi.lenovo.asia5b.app_ui.profile.activity.ChangeAddressActivity;
import com.wushi.lenovo.asia5b.app_ui.profile.activity.NewAddressActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

import static java.lang.String.format;

/**
 * Created by zhangyuncai on 2020/3/5.
 */
public class OrderAddressView extends LinearLayout {
    @BindView(R.id.tv_order_name)
    TextView tv_order_name;
    @BindView(R.id.tv_order_address)
    TextView tv_order_address;
    @BindView(R.id.tv_order_mobile)
    TextView tv_order_mobile;
    @BindView(R.id.tv_order_edit)
    ImageView tv_order_edit;
    @BindView(R.id.bt_order_newaddres)
    Button bt_order_newaddres;
    @BindView(R.id.ll_order_newaddres_vessel)
    LinearLayout ll_order_newaddres_vessel;

    //收货地址
    private AddressModel addressModel;

    public OrderAddressView(Context context) {
        super(context);
        init();
    }

    public OrderAddressView(Context context, @Nullable @android.support.annotation.Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public OrderAddressView(Context context, @Nullable @android.support.annotation.Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        View.inflate(getContext(), R.layout.view_orderaddress2, this);
        ButterKnife.bind(this);
        ll_order_newaddres_vessel.setOnClickListener(v -> {
        });
        bt_order_newaddres.setOnClickListener(v -> {
            startNewAddressActivity();
        });
        tv_order_edit.setOnClickListener(v -> {
            getContext().startActivity(new Intent(getContext(), ChangeAddressActivity.class));
        });
    }

    private void startNewAddressActivity() {
        Intent intent = new Intent(getContext(), NewAddressActivity.class);
        intent.putExtra("OrderPayment", true);
        getContext().startActivity(intent);
    }


    /**
     * 设置地址视图
     */
    public void setAddressView(AddressModel addressModel) {
        this.addressModel = addressModel;
        /*收货地址 ll_order_newaddres_vessel*/
        if (addressModel == null) {
            ll_order_newaddres_vessel.setVisibility(View.VISIBLE);
        } else {
            ll_order_newaddres_vessel.setVisibility(View.GONE);
            //名字 tv_order_name
            tv_order_name.setText(addressModel.getContact_name());
            //地址 tv_order_address
            tv_order_address.setText(addressModel.getLocalAddressDetail());
            //电话 tv_order_mobile
            String phone = format("+%s%s", addressModel.getCalling_code() + "", addressModel.getContact_no());
            tv_order_mobile.setText(phone);
        }
    }


    public AddressModel getAddressModel() {
        return addressModel;
    }
}
